package com.project.web.admin.controller.core;

import com.project.model.core.Area;
import com.project.web.admin.controller.common.BaseController;
import com.project.web.admin.service.core.AreaService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yun.common.utils.appInterface.Message;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("adminAreaController")
@RequestMapping("/admin/area")
public class AreaController extends BaseController {

    private static final long serialVersionUID = 2697020601502119750L;

    @Resource(name = "areaServiceImpl")
    private AreaService areaService;

    /**
     * 树形页面入口
     */
    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    public String tree() {
        return "/intfc/core/area/tree.ftl";
    }

    /**
     * 异步获取节点
     */
    @ResponseBody
    @RequestMapping(value = "/getTree", method = RequestMethod.GET)
    public List<Area> getTree(Long id) {

        if (null != id) {
            List<Area> childrenNode = areaService.findChildrenRootNodes(id);
            if (CollectionUtils.isNotEmpty(childrenNode)) {
                return childrenNode;
            } else {
                return areaService.findOnlyRoots();
            }

        } else {
            List<Area> list = areaService.findOnlyRoots();
            return areaService.findOnlyRoots();
        }

    }

    /**
     * 添加
     *
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() {
        return "/intfc/core/area/add.ftl";
    }

    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Map<String, Object> save(Area area, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        areaService.save(area);
        map.put("success", true);
        map.put("type", Message.Type.success.toString());
        map.put("msg", "保存成功!");
        map.put("data", area);
        return map;
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, Model model) {
        Area area = areaService.getById(id);
        if (area != null) {
            model.addAttribute("area", area);
        }
        return "/intfc/core/area/edit.ftl";

    }

    /**
     * 更新
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Map<String, Object> update(Area area, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        areaService.updateNodes(area);
        map.put("success", true);
        map.put("type", Message.Type.success.toString());
        map.put("msg", "保存成功!");
        map.put("data", area);
        return map;
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Map<String, Object> delete(Long id) {

        Area area = areaService.getById(id);
        Map<String, Object> map = new HashMap<String, Object>();
        Area areatemp = new Area();
        areatemp.setParentId(id);
        List<Area> areas = areaService.selectList(areatemp);
        if (CollectionUtils.isNotEmpty(areas)) {
            map.put("success", false);
            map.put("type", Message.Type.warn.toString());
            map.put("msg", "存在子节点，不能删除！");
            return map;
        }
        if (area != null) {
            areaService.delete(id);
            map.put("success", true);
            map.put("type", Message.Type.success.toString());
            map.put("msg", "删除成功!");
            return map;
        } else {
            map.put("success", false);
            map.put("type", Message.Type.warn.toString());
            map.put("msg", "要操作的对象不存在或已被删除!");
            return map;
        }
    }


}
